var sd = require("silly-datetime");
var connection = require("./connect")


exports.register = function (data, callback) {
      if (data.username.length <= 0 || data.username == '') {
            return callback(null, 1)
      } else if (data.password.length <= 0 || data.password == '') {
            return callbacka(null, 2)
      } else if (data.nick.lenth <= 0 || data.nick == '') {
            return callback(null, 3)
      } else if (data.username.length < 6 || data.username.length > 16) {
            return callback(null, 4)
      } else if (data.password.length < 6 || data.password.length > 16) {
            return callback(null, 5)
      } else if (data.repassword != data.repassword) {
            return callback(null, 6)
      } else if (data.nick.length < 2 || data.nick.length > 10) {
            return callback(null, 7)
      } else {
            connection.query(`select count(username) as count from users where username="${data.username}";`, function (err, user) {
                  if (err) return callback(err)
                  if (user[0].count > 0 || user[0].count != 0) {
                        console.log(user)
                        return callback(null, 8)
                  } else {
                        connection.query(`select count(nick) as count from users where nick="${data.nick}";`, function (err, user) {
                              if (err) return callback(err)
                              if (user[0].count > 0 || user[0].count != 0) {
                                    return callback(null, 9)
                              } else {
                                    data.time = sd.format(new Date, 'YYYY-MM-DD HH:mm:ss')
                                    connection.query(`insert into users values(null,"${data.username}","${data.nick}","${data.password}","${data.time}");`, function (err) {
                                          if (err) return callback(err)
                                          return callback(null, 0)
                                    })
                              }
                        })
                  }
            })

      }
}



exports.login = function (user, callback) {
      if (user.username.length <= 0 || user.username == '') {
            return callback(null, 1)
      } else if (user.password.length <= 0 || user.password == '') {
            return callbacka(null, 2)
      } else if (user.username.length < 6 || user.username.length > 16) {
            return callback(null, 4)
      } else if (user.password.length < 6 || user.password.length > 16) {
            return callback(null, 5)
      } else {
            connection.query(`select count(*) as count from users where username="${user.username}" and password="${user.password}";`, function (err, data) {
                  if (err) throw err
                  if (data[0].count <= 0) {
                        return callback(null, 9)
                  } else if (data[0].count >= 1) {
                        connection.query(`select nick from users where username="${user.username}" and password="${user.password}";`, function (err, user) {
                              if (err) throw err
                              return callback(null, 0, user[0].nick)
                        })
                  }
            })
      }
}
